import com.google.appengine.api.datastore.*
import static com.google.appengine.api.datastore.FetchOptions.Builder.*

if(params.transcationKey != null) {
	def ids = []
	if(params.transcationKey instanceof String){
		ids << params.transcationKey
	} else {
		ids = params.transcationKey
	}
	ids.each { tid ->
		int transcationId = tid as int
		def transcationKey = [
			'TranscationE',
			transcationId] as Key
		Entity transcation = datastore.get(transcationKey)
		def query = new Query('TranscationTagE')
		query.addSort('name', Query.SortDirection.ASCENDING)
		query.addFilter('transcationId', Query.FilterOperator.EQUAL, transcation.id)
		query.addFilter('userId', Query.FilterOperator.EQUAL, session.user.id)
		PreparedQuery preparedQuery = datastore.prepare(query)
		def ttentities = preparedQuery.asList( withLimit(Integer.MAX_VALUE) )
		ttentities.each { transcationTag ->
			transcationTag.delete()
		}
		transcation.delete()
	}
}
session.removeAttribute('todayTranscations')
session.removeAttribute('weekTranscations')
session.removeAttribute('monthTranscations')

def infoMessage = "Transcation '$params.transcationKey' are deleted..."
if(params.json == null) {
	request.infoMessage = infoMessage

	forward 'transcationJQuery.groovy'
}
else {
	println infoMessage
}
